package vip.xiaonuo.biz.modular.Englishword;

import org.apache.commons.lang3.StringUtils;
import vip.xiaonuo.biz.modular.Englishword.result.EnglishArticleResult;
import vip.xiaonuo.biz.modular.Englishword.result.EnglishArticleWithQuestionsResult;
import vip.xiaonuo.biz.modular.Englishword.result.EnglishMultipleChoiceQuestionResult;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordUtils {


    public static void main(String[] args) {

        List<EnglishArticleWithQuestionsResult> results = parseText("《文章-!Subject!》\n" +
                "In a small studio near the heart of the city, a group of undergraduate students gathered to discuss the misconceptions about the enforcement of rules in recreational activities. The studio was filled with the latest technology, including powerful generators and advanced battery systems, to support their projects. One of the students, considering the recent explosion of misinformation on social media, proposed creating a platform that would outline the proper format for sharing information about these activities. They believed this could help clarify the single biggest misconception: that the emperor's decrees were strictly enforced without any room for interpretation.\n" +
                "\n" +
                "The student explained that the foundation of understanding such activities lay not only in knowing the rules but also in grasping the context behind them. He argued that the current system often failed because it did not allow for oral explanations or personal interpretations. Instead, it prescribed rigid guidelines that left no room for flexibility or individual understanding.\n" +
                "\n" +
                "Another student countered, suggesting that the best way to enforce these rules would be through strict regulations, similar to those used in enforcing laws. However, the first student disagreed, emphasizing that this approach would only lead to more misconceptions and confusion among participants. He insisted that the key was to foster an environment where individuals could explore and understand the rules on their own terms, much like learning through oral tradition.\n" +
                "\n" +
                "As they concluded their discussion, the group agreed to start by developing a basic outline of their project. They hoped that this generator of ideas would eventually lead to a comprehensive output that could be shared with other students and enthusiasts, thereby promoting a better understanding of the recreational activities and dispelling common misconceptions.\n" +
                "\n" +
                "The studio buzzed with excitement as they began their work, each member contributing their unique perspective and skills. They knew that their efforts could make a difference, not just for themselves, but for the broader community, fostering a spirit of charity and mutual respect.\n" +
                "\n" +
                "《文章-!Option!》\n" +
                "Choose correct answers to the question:\n" +
                "1. What is the main goal of the students' project?\n" +
                "A. To create a platform for sharing information about recreational activities.\n" +
                "B. To enforce strict regulations on recreational activities.\n" +
                "C. To build a new studio equipped with advanced technology.\n" +
                "D. To prescribe rigid guidelines for all activities.\n" +
                "2. According to the first student, what is crucial for understanding recreational activities?\n" +
                "A. Knowing the rules without context.\n" +
                "B. Strict regulations and enforcement.\n" +
                "C. Oral explanations and personal interpretation.\n" +
                "D. Focusing solely on the emperor's decrees.\n" +
                "3. What does the second student suggest as the best way to enforce rules?\n" +
                "A. By fostering an environment for exploration.\n" +
                "B. Through strict regulations.\n" +
                "C. By relying on oral tradition.\n" +
                "D. By allowing individual interpretations.\n" +
                "4. How do the students plan to start their project?\n" +
                "A. By developing a comprehensive output.\n" +
                "B. By building a new studio.\n" +
                "C. By creating a detailed outline.\n" +
                "D. By enforcing strict guidelines.\n" +
                "5. What additional benefit do the students hope their project will have?\n" +
                "A. It will lead to stricter regulations.\n" +
                "B. It will dispel common misconceptions and promote mutual respect.\n" +
                "C. It will increase the use of advanced technology.\n" +
                "D. It will focus on the emperor's decrees.\n" +
                "\n" +
                "《文章-!Answer!》\n" +
                "1.A\n" +
                "2.C\n" +
                "3.B\n" +
                "4.C\n" +
                "5.B\n" +
                "《文章-!End!》");
        String text = "In recent years, the evolution of mobile technology has brought about many changes in our daily lives. From smartphones to tablets, these devices have become essential tools for communication and entertainment. However, one of the most significant changes has been the way we access information. With the rise of multiple channels, people can now access news and updates from various sources, including social media platforms, news websites, and mobile applications. This has made it easier for individuals to stay informed about current events and developments around the world.\n" +
                "\n" +
                "One notable example is the use of mobile applications for weather forecasting. These apps provide real-time updates on weather conditions, helping users prepare for potential explosions or other natural disasters. For instance, a pilot flying over a stormy region can use such an app to find the safest route to their destination. Similarly, individuals living in areas prone to earthquakes can use these apps to stay updated on seismic activity and take necessary precautions.\n" +
                "\n" +
                "However, with the increased reliance on technology, there are also concerns about data privacy and security. Many users are hesitant to share personal information online due to the fear of exposure to unauthorized parties. To address this issue, companies are implementing more robust security measures, such as encryption and multi-factor authentication. Additionally, there is a growing demand for up-to-date information, which has led to the development of more sophisticated translation tools that can provide real-time translations of foreign language content.\n" +
                "\n" +
                "Despite these advancements, some individuals still feel overwhelmed by the amount of information available. They worry about being able to concentrate on important tasks amidst the constant influx of notifications and alerts. In response, developers are creating apps that help users manage their time more effectively, allowing them to focus on specific tasks without distractions.\n" +
                "\n" +
                "Overall, the rapid evolution of mobile technology has transformed the way we access and process information. While there are challenges associated with this transformation, the benefits of staying connected and informed outweigh the drawbacks.\n" +
                "\n";
        Set<String> wordsToCount = new HashSet<>(Arrays.asList(
                "pilot", "umbrella", "approval", "mobile", "perform", "multiple", "peak", "evolution", "portrait",
                "halfway", "expose", "concentrate", "magnet", "explosion", "weld", "up-to-date", "complaint",
                "translation", "cancer", "personnel", "hopeless", "outlook", "fountain", "breadth", "channel",
                "focus", "invisible", "entire", "pea", "pill", "wrist"
        ));
        double relevanceScore = calculateRelevanceScore(text, wordsToCount);
        System.out.printf("《相关度指标》\n%.2f\n", relevanceScore);
        String s = formatQuestions("《文章-!Subject!》\n" +
                "In the world of scientific exploration, the telescope has played an indispensable role in expanding our understanding of the universe. Recently, astronomers have been using advanced telescopes to observe distant galaxies and celestial bodies, gaining unique insights into the mysteries of space. The development of telescopes has not only provided scientists with the ability to see beyond what is visible to the naked eye but also equipped them with tools to calculate distances and sizes of celestial objects accurately. Telescopes have helped researchers detect X-rays and other forms of radiation from stars and black holes, which are often hidden by cosmic dust and gas. This has led to unexpected discoveries that challenge conventional theories about the universe. In tropical regions, where atmospheric conditions can be challenging for ground-based telescopes, observatories are strategically placed at high altitudes to minimize interference. These advancements in technology have allowed us to conquer some of the limitations imposed by Earth's atmosphere, opening up new frontiers in astronomy. However, the use of telescopes also raises concerns about the potential impact on local ecosystems and preventive measures must be taken to ensure that these instruments do not disrupt natural habitats or provoke environmental chaos.\n" +
                "\n" +
                "《文章-!Option!》\n" +
                "Choose correct answers to the question:\n" +
                "1.The primary function of telescopes in astronomy is to __________.\n" +
                "A.provide entertainment for amateur stargazers\n" +
                "B.detect X-rays and other forms of radiation from celestial bodies\n" +
                "C.measure the mental health of astronauts\n" +
                "D.calculate the ratio of cancer cells in humans\n" +
                "2.The word \"indispensable\" in the passage most likely means __________.\n" +
                "A.unnecessary B.unique C.essential D.expensive\n" +
                "3.Advanced telescopes have enabled scientists to gain unique insights into the universe by allowing them to __________.\n" +
                "A.skim through scientific journals quickly\n" +
                "B.observe distant galaxies and celestial bodies\n" +
                "C.directly communicate with extraterrestrial beings\n" +
                "D.soak up more sunlight during the day\n" +
                "4.What concern does the passage mention regarding the placement of telescopes in tropical regions?\n" +
                "A.They may cause a boom in tourism.\n" +
                "B.They might disrupt local ecosystems.\n" +
                "C.They can prevent the growth of germs.\n" +
                "D.They will create more jobs for editors.\n" +
                "5.Which of the following best describes the impact of telescopes on our understanding of the universe?\n" +
                "A.They have provoked unexpected discoveries and challenged conventional theories.\n" +
                "B.They have made it easier to ban smoking in public areas.\n" +
                "C.They have reduced the need for preventive healthcare measures.\n" +
                "D.They have caused a sore throat among researchers.\n" +
                "\n" +
                "《文章-!Answer!》\n" +
                "1.B\n" +
                "2.C\n" +
                "3.B\n" +
                "4.B\n" +
                "5.A\n" +
                "\n" +
                "《文章-!End!》\n" +
                "\n" +
                "《文章-!Subject!》\n" +
                "The role of editors in shaping modern media cannot be overstated. Editors play a crucial role in maintaining the quality and integrity of content across various platforms. They ensure that articles and reports adhere to journalistic conventions and standards. One of the key responsibilities of an editor is to review and refine written material, ensuring that it is clear, concise, and free from errors. Editors must also be equipped with the skills to handle confidential information responsibly, as they often deal with sensitive data before publication. In today’s fast-paced digital age, editors face the challenge of balancing speed and accuracy. They must skim through large volumes of information quickly to meet deadlines without compromising on quality. Moreover, editors must provide constructive feedback to writers, offering insight into how to improve their work. Editors also play a vital role in preventing misinformation by fact-checking and verifying sources. With the rise of social media, the importance of reliable editorial oversight has become even more apparent. Editors must navigate the complex landscape of external influences and maintain a neutral stance to avoid provoking controversy. In addition, editors must be adaptable to the ever-changing media environment, adopting new technologies and practices to stay relevant. Ultimately, the work of editors is indispensable in maintaining the credibility and trustworthiness of media outlets.\n" +
                "\n" +
                "《文章-!Option!》\n" +
                "Choose correct answers to the question:\n" +
                "1.What is one of the key responsibilities of an editor according to the passage?\n" +
                "A.To design book covers\n" +
                "B.To review and refine written material\n" +
                "C.To calculate financial ratios\n" +
                "D.To promote products directly\n" +
                "2.The word \"confidential\" in the passage most likely means __________.\n" +
                "A.public B.secret C.common D.direct\n" +
                "3.Why must editors be able to skim through large volumes of information quickly?\n" +
                "A.To meet deadlines without compromising on quality\n" +
                "B.To soak up more sunlight\n" +
                "C.To prevent the spread of germs\n" +
                "D.To restrain public protests\n" +
                "4.How do editors contribute to preventing misinformation?\n" +
                "A.By fact-checking and verifying sources\n" +
                "B.By banning all social media platforms\n" +
                "C.By calculating the ratio of cancer cases\n" +
                "D.By creating more mental health awareness\n" +
                "5.What does the passage suggest about the adaptability of editors?\n" +
                "A.They must adopt new technologies and practices to stay relevant.\n" +
                "B.They should focus solely on traditional methods.\n" +
                "C.They should ban all external influences.\n" +
                "D.They should prevent any changes in the media environment.\n" +
                "\n" +
                "《文章-!Answer!》\n" +
                "1.B\n" +
                "2.B\n" +
                "3.A\n" +
                "4.A\n" +
                "5.A\n" +
                "\n" +
                "《文章-!End!》\n" +
                "\n" +
                "《文章-!Subject!》\n" +
                "Mental health has become an increasingly important topic in recent years, with growing recognition of its impact on overall well-being. Mental health issues such as anxiety and depression can lead to physical symptoms like a sore throat or acid reflux, highlighting the interconnectedness of mind and body. Preventive measures, including regular exercise and a balanced diet, can help manage mental health conditions. Leaders in the field of psychology emphasize the importance of creating supportive environments that foster mental resilience. A healthy atmosphere at home, school, or work can significantly reduce stress levels and promote emotional stability. Recent studies have shown that mental health disorders can affect people of all ages, races, and backgrounds, making it a universal concern. Unexpected events, such as job loss or personal tragedy, can exacerbate mental health issues, underscoring the need for comprehensive support systems. Mental health professionals advocate for routine check-ups and early intervention to address problems before they escalate. Additionally, innovative treatments like cognitive-behavioral therapy (CBT) offer promising solutions for those struggling with mental health challenges. Understanding the unique character of each individual’s experience is crucial for providing personalized assistance. By fostering a culture of openness and empathy, we can break down the stigma surrounding mental health and encourage more people to seek help when needed. Preventive healthcare measures, such as promoting mental wellness programs, can also play a significant role in reducing the prevalence of mental health issues in society.\n" +
                "\n" +
                "《文章-!Option!》\n" +
                "Choose correct answers to the question:\n" +
                "1.Mental health issues can lead to physical symptoms such as __________.\n" +
                "A.a sore throat and acid reflux\n" +
                "B.high temperatures and tropical storms\n" +
                "C.infinite wealth and prosperity\n" +
                "D.a reasonable ratio of cancer cases\n" +
                "2.Preventive measures for managing mental health conditions include __________.\n" +
                "A.regular exercise and a balanced diet\n" +
                "B.calculating financial ratios\n" +
                "C.banning all social interactions\n" +
                "D.soaking in hot springs\n" +
                "3.What do leaders in the field of psychology emphasize about mental health?\n" +
                "A.The importance of creating supportive environments that foster mental resilience\n" +
                "B.The need to increase the number of telescopes\n" +
                "C.The necessity of restraining public protests\n" +
                "D.The significance of promoting external influences\n" +
                "4.Unexpected events such as job loss can __________.\n" +
                "A.exacerbate mental health issues\n" +
                "B.create more jobs for editors\n" +
                "C.reduce the prevalence of cancer\n" +
                "D.prevent the spread of germs\n" +
                "5.What is crucial for providing personalized assistance in mental health?\n" +
                "A.Understanding the unique character of each individual’s experience\n" +
                "B.Calculating the ratio of mental health disorders\n" +
                "C.Directly addressing external factors\n" +
                "D.Skimming through medical journals quickly\n" +
                "\n" +
                "《文章-!Answer!》\n" +
                "1.A\n" +
                "2.A\n" +
                "3.A\n" +
                "4.A\n" +
                "5.A\n" +
                "\n" +
                "《文章-!End!》");
        System.out.println(s);
    }

    /**
     * @param input
     * @return {@link String}
     */
    private static String formatQuestions(String input) {
        return input.replaceAll("(?<=\\d\\.\\D*) (?=[ABCD]\\.)", "\n")
                .replaceAll("(?<=\\d\\.\\D* [ABCD]\\..*?) (?=[ABCD]\\.)", "\n");
    }


    /**
     * 计算相关性得分
     *
     * @param text         文本
     * @param wordsToCount 要数单词
     * @return double
     */
    private static double calculateRelevanceScore(String text, Set<String> wordsToCount) {
        // 去除标点符号，转换为小写，分词
        List<String> words = extractWords(text);

        // 去除常见简单单词
        Set<String> simpleWords = new HashSet<>(Arrays.asList(
                "be", "am", "is", "are", "you", "i", "it", "the", "and", "of", "to", "in", "a", "for", "on", "with",
                "as", "by", "that", "this", "an", "at", "but", "or", "from", "not", "have", "has", "had", "will",
                "shall", "may", "can", "could", "would", "should", "might", "must", "do", "does", "did", "we", "they",
                "he", "she", "which", "who", "whom", "whose", "what", "how", "when", "where", "why","was", "were", "being", "been", "let", "without", "into", "out", "up", "down", "over", "under", "through",
                "about", "after", "before", "between", "among", "during", "until", "while", "like", "than", "such", "these",
                "those", "its", "me", "him", "them", "us", "my", "your", "his", "our", "their", "all", "any", "some", "many",
                "much", "few", "little", "one"
        ));


        // 预处理：去重和去除简单单词
        Set<String> filteredWords = new HashSet<>();
        for (String word : words) {
            if (!simpleWords.contains(word)) {
                filteredWords.add(word);
            }
        }

        // 计算指定单词出现的次数
        int specifiedWordCounts = 0;
        for (String word : wordsToCount) {
            if (filteredWords.contains(word)) {
                specifiedWordCounts += Collections.frequency(words, word);
            }
        }

        // 计算相关度指标
        return (double) specifiedWordCounts / filteredWords.size();
    }

    private static List<String> extractWords(String text) {
        List<String> words = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\b\\w+\\b");
        Matcher matcher = pattern.matcher(text.toLowerCase());

        while (matcher.find()) {
            words.add(matcher.group());
        }

        return words;
    }

    // ---------------------------------------
    // 文章解析相关 Start
    // ---------------------------------------
    /**
     * 解析文本
     *
     * @param text 文本
     * @return {@link List}<{@link EnglishArticleWithQuestionsResult}>
     */
    public static List<EnglishArticleWithQuestionsResult> parseText(String text) {
        if (StringUtils.isBlank(text)) {
            return Collections.emptyList();
        }
//        text=preprocessText(text);
        List<EnglishArticleWithQuestionsResult> articles = new ArrayList<>();

        // 使用《文章-LancerEnd》作为分隔符分割各个文章片段
        String[] articleSegments = text.split("《文章-!End!》");

        for (String segment : articleSegments) {
            if (!segment.trim().isEmpty()) { // 确保不处理空的段落
                // 分割并处理每个部分
                String subjectPart = "", optionPart = "", answerPart = "";

                if (segment.contains("《文章-!Subject!》")) {
                    subjectPart = segment.split("《文章-!Subject!》")[1].split("《")[0].trim();
                }
                if (segment.contains("《文章-!Option!》")) {
                    optionPart = segment.split("《文章-!Option!》")[1].split("《")[0].trim();
                }
                if (segment.contains("《文章-!Answer!》")) {
                    answerPart = segment.split("《文章-!Answer!》")[1].split("《")[0].trim();
                }

                // 创建Article对象
                EnglishArticleResult article = new EnglishArticleResult("", subjectPart);

                // 解析选项部分
                List<EnglishMultipleChoiceQuestionResult> questions = parseOptions(optionPart);

                // 解析答案部分，并设置正确答案到问题中
                parseAnswers(answerPart, questions);

                // 将解析好的文章和问题列表添加到结果集中
                articles.add(new EnglishArticleWithQuestionsResult(article, questions));
            }
        }

        return articles;
    }

    /**
     * 解析选项
     *
     * @param optionsText 选项文本
     * @return {@link List}<{@link EnglishMultipleChoiceQuestionResult}>
     */
    private static List<EnglishMultipleChoiceQuestionResult> parseOptions(String optionsText) {
        // 预处理，格式化问题比如：所有的选项都在一行的情况
        optionsText = formatQuestions(optionsText);
        List<EnglishMultipleChoiceQuestionResult> questions = new ArrayList<>();
        String[] lines = optionsText.split("\n");

        EnglishMultipleChoiceQuestionResult currentQuestion = null;
        List<String> currentOptions = new ArrayList<>();

        for (String line : lines) {
            line = line.trim();
            if (line.startsWith("1.") || line.startsWith("2.") || line.startsWith("3.") || line.startsWith("4.") || line.startsWith("5.")) {
                // 如果当前有构建中的问题，则先加入列表
                if (currentQuestion != null) {
                    currentQuestion.setOptions(currentOptions);
                    questions.add(currentQuestion);
                    currentOptions = new ArrayList<>();
                }
                // 新的问题开始
                String question = line.substring(line.indexOf('.') + 1).trim();
                currentQuestion = new EnglishMultipleChoiceQuestionResult(question, new ArrayList<>(), ' ');
            } else if (line.matches("[A-D]\\..*")) {
                currentOptions.add(line.substring(2).trim());
            }
        }
        // 添加最后一个问题
        if (currentQuestion != null) {
            currentQuestion.setOptions(currentOptions);
            questions.add(currentQuestion);
        }
        return questions;
    }

    /**
     * 解析答案
     *
     * @param answersText 答案文本
     * @param questions   问题
     */
    private static void parseAnswers(String answersText, List<EnglishMultipleChoiceQuestionResult> questions) {
        String[] lines = answersText.split("\n");
        for (String line : lines) {
            char answer = line.charAt(line.length() - 1);
            int questionIndex = Character.getNumericValue(line.charAt(0)) - 1;
            if (questionIndex >= 0 && questionIndex < questions.size()) {
                questions.get(questionIndex).setCorrectAnswer(answer);
            }
        }
    }
    private static String preprocessText(String text) {
        // 移除多余的空白字符
        text = text.replaceAll("\\s+", " ").trim();
        // 统一换行符为\n
        text = text.replace("\r\n", "\n").replace("\r", "\n");
        return text;
    }
    // ---------------------------------------
    // 文章解析相关 END
    // ---------------------------------------


}

